import {
	IParam,
	IExpCtrlAbility,
	IExpViewAbility,
	ExpViewActionType,
	IExpViewController,
	IExpViewControllerParams,
	IExpViewStore,
	ExpCtrlActionType,
	INavigateParam,
	IViewActionResult,
    IViewAbility,
} from '@/core/interface';
import { deepCopy } from '@/core/utils';
import { DEViewController } from './de-view-controller';

/**
 * 导航视图控制器
 *
 * @export
 * @class ExpViewController
 * @extends {(DEViewController<T | ExpViewActionType, S, A>)}
 * @implements {IExpViewController<S, A>}
 * @template T
 * @template S
 * @template A
 */
export class ExpViewController<
	T,
	S extends IExpViewStore,
	A extends IExpViewAbility
>
	extends DEViewController<T | ExpViewActionType, S, A>
	implements IExpViewController<S, A>
{
	/**
	 * 查询参数
	 *
	 * @protected
	 * @type {IParam}
	 * @memberof ExpViewController
	 */
	protected queryParams: IParam = {};

	/**
	 * 是否抛出快速分组值
	 *
	 * @private
	 * @type {boolean}
	 * @memberof ExpViewController
	 */
	private isEmitQuickGroupValue = false;

	/**
	 * 是否关闭导航视图
	 *
	 * @private
	 * @type {boolean}
	 * @memberof ExpViewController
	 */
	private isCloseNavView = false;

	/**
	 * 处理视图初始化
	 *
	 * @protected
	 * @param {IExpViewControllerParams<T | ExpViewActionType, A>} params
	 * @memberof ExpViewController
	 */
	protected processViewInit(
		params: IExpViewControllerParams<T | ExpViewActionType, A>
	) {
		super.processViewInit(params);
	}

	/**
	 * 获取导航部件能力
	 *
	 * @protected
	 * @return {*}  {(IExpCtrlAbility | undefined)}
	 * @memberof ExpViewController
	 */
	protected getMainCtrlAbility(): IExpCtrlAbility | undefined {
		throw new Error('Method is no');
	}

    /**
     * 导航视图能力
     *
     * @protected
     * @return {*}  {(IViewAbility | undefined)}
     * @memberof ExpViewController
     */
    protected getExpViewAbility(): IViewAbility | undefined {
        if (this.store.navParam && this.store.navParam.navViewName) {
			return this.getSubAbility<IViewAbility>(this.store.navParam.navViewName);
		}
		return undefined;
    }

	/**
	 * 处理部件行为
	 *
	 * @param {string} name
	 * @param {string} action
	 * @param {IParam[]} data
	 * @memberof ExpViewController
	 */
	public handleCtrlAction(name: string, action: string, data: IParam[]): void {
		super.handleCtrlAction(name, action, data);
		const expBar = this.getMainCtrlAbility();
		if (expBar && expBar.name === name) {
			this.handleExpCtrlAction(action, data);
		}
	}

	/**
	 * 视图挂载
	 *
	 * @param {IParam} [opts={}]
	 * @return {*}  {void}
	 * @memberof ExpViewController
	 */
	public viewMounted(opts: IParam = {}): void {
		super.viewMounted(opts);
		if (this.model.useDefaultLayout) {
			if (!this.isLoadDefault) {
				this.isLoadDefault = true;
				return;
			}
			this.load(opts);
		} else {
			this.initLayout().then(() => {
                if (!this.isLoadDefault) {
                    this.isLoadDefault = true;
                    return;
                }
				this.load(opts);
			});
		}
	}

    /**
     * 刷新
     * 
     * @param {IParam} [args]
     * @return {*}  {Promise<boolean>}
     * @memberof ExpViewController
     */
    public async refresh(args?: IParam): Promise<boolean> {
        await this.getMainCtrlAbility()?.refresh(args);
		return await this.getExpViewAbility()?.refresh(args) || false;
	}
    
	/**
	 * 视图加载
	 *
	 * @param {IParam} [args]
	 * @return {*}  {Promise<IViewActionResult>}
	 * @memberof ExpViewController
	 */
	public load(args?: IParam): Promise<IViewActionResult> {
		const expCtrl = this.getMainCtrlAbility();
		if (expCtrl) {
			const tempViewParams = deepCopy(this.store.viewParams);
			return expCtrl.load(Object.assign(tempViewParams, args));
		} else {
			return Promise.reject({
				ok: false,
				data: null,
				rowData: { status: 500 },
			});
		}
	}

	/**
	 * 处理导航部件行为
	 *
	 * @template T
	 * @param {(T | ExpCtrlActionType)} action
	 * @param {IParam[]} data
	 * @memberof ExpViewController
	 */
	public handleExpCtrlAction<T>(action: T | ExpCtrlActionType, data: IParam[]) {
		if (action === 'selectionChange') {
			this.handleSelectionChange(data as INavigateParam[]);
		}
		if (action === 'beforeLoad') {
			this.handleMDCtrlBeforeLoad(data);
		}
	}

	/**
	 * 处理导航栏部件选中数据变更
	 *
	 * @protected
	 * @param {INavigateParam[]} param
	 * @return {*}
	 * @memberof ExpViewController
	 */
	protected handleSelectionChange(param: INavigateParam[]) {
		if (!param || param.length === 0) {
			return;
		}
		if (this.isCloseNavView) {
			return;
		}
		this.store.navParam = param[0];
		if (!this.model.useDefaultLayout) {
			const navPos: any = Object.values(this.store.layoutModelDetails).find(
				(item: any) => {
					return item.predefinedType === 'NAV_POS';
				}
			);
			if (navPos) {
				navPos.setNavData(param[0]);
			}
		}
		this.emit('viewDataChange', [param[0].data]);
	}

	/**
	 * 处理多数据部件加载之前
	 *
	 * @protected
	 * @param {IParam[]} data
	 * @memberof MDViewController
	 */
	protected handleMDCtrlBeforeLoad(data: IParam[]) {
		const param = data && data.length > 0 ? data[0] : {};
		if (this.queryParams && Object.keys(this.queryParams).length > 0) {
			if (this.queryParams['quickGroupData']) {
				Object.assign(param, this.queryParams['quickGroupData']);
			}
		}
	}

	/**
	 * 快速分组值变化
	 *
	 * @param {IParam} item
	 * @memberof ExpViewController
	 */
	public handleQuickGroupValueChange(item: IParam) {
		if (item) {
			this.queryParams.quickGroupData = item.data;
			const expCtrl = this.getMainCtrlAbility();
			if (expCtrl) {
				const tempViewParams = deepCopy(this.store.viewParams);
				expCtrl.load(tempViewParams);
			}
		}
		this.isEmitQuickGroupValue = true;
	}
}
